/*
 ============================================================================
 Name        : test_rio.c
 Author      : 11
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <semaphore.h>
#include <error.h>
#include <malloc.h>
#include <netdb.h>
#include <netinet/in.h>
#include <assert.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include "riocpplib.h"


#if 1
void recvtest()
{
	unsigned int *pSrc;
	int ret=0;
	ret=bslRioRecvData(0x302000000,0x200000);
	while(1)
	{
		bslWaitInputSrioData();
		pSrc= (unsigned int*)bslRecvDatabuf;
		printf("SRIO RECV DATA APP Header %x %x %x %x %x\n",pSrc[0],pSrc[1],pSrc[2],pSrc[3],pSrc[4]);
	}
}
#endif

int main(void) {
	int ret=0;
	int i=0;
	unsigned int SRIO_ID;
	unsigned int *pSrc;
	char buf[2048];
	cpu_set_t cpu_info;

	pthread_t xdma_t1[5];
	pthread_attr_t attr1[5];

	ret=bslRioPortInit(0);
	if(ret==0)
	{
		printf("INIT SRIO PORT DONE\n");
	}
	else
	{
		printf("INIT SRIO PORT FAILED\n");
		return -1;
	}

//	SRIO_ID=(bslRioGetID()>>16)&0xf;
	SRIO_ID=bslRioGetID();
	printf("SRIO ID is 0x%x\n",SRIO_ID);


#if 1
	ret=bslRioSendDoorbell(0x3, 0x55aa);
	if(ret<0)
	{
		printf("SEND DOORBELL FAILED\n");
		return -1;
	}
	else
	{
		printf("SEND DOORBELL DONE\n");
	}
#endif

	for(i=0;i<2048;i++)
	{
		buf[i]=i;
	}
#if 1
	ret=bslRioSendData(0x3, 0x2000000, buf,2048);
	if(ret<0)
	{
		printf("SEND SRIO DATA FAILED\n");
		return -1;
	}
	else
	{
		printf("SEND SRIO DATA DONE\n");
	}
#endif

	if(0 != pthread_create(&xdma_t1,NULL,(void*)recvtest,NULL) )
	{
		printf("FILE:%s LINE:%d FUNC: THREAD Error!\n",__FILE__,__LINE__);
		return -1;
	}
	else
	{
		printf("%s %d Cmp: THREAD Started!\n",__FILE__,__LINE__);
	}

#if 1
	printf("Recv doorbell test begin\n");
	ret=bslRioRecvDoorbell(0x3);
#endif


	while(1)
	{
		sleep(1);
	}


	return EXIT_SUCCESS;
}
